package com.ali.gts.servlet.controller;

import com.ali.gts.servlet.service.JudgeTrueOrFalse;
import com.ali.gts.servlet.util.CookieUtil;
import com.ali.gts.servlet.util.WebUtil;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Optional;
import java.util.function.Supplier;

/**
 *
 *  我们后端给前端返回信息由很多中方式，利用ajax、各种前端的框架等
 *  但是后端怎么给浏览器发送信息呢，这里就用到了Cookie了。这个和使用什么前端框架没有关系。
 *  浏览器收到Cookie 后会保存起来
 *
 *  服务器怎么获取浏览器传回来的Cookie 呢
 *  以后浏览器向后端发起请求，请求头里面都会有cookie 的信息
 *
 */
public class CookieServlet extends BaseServlet {

    Supplier<String> supplier = () -> {
        return "key1";
    };

    Supplier<String> supplier1 = () -> {
        return "key2";
    };

    protected void crateCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1 创建Cookie
        Cookie cookie = new Cookie("key1", "value1");
        Cookie cookie2 = new Cookie("key2", "value2");
        //2、服务端通知浏览器保存cookie,都是通过响应来操作的,这里千万不能省略,添加了两个cookie
        resp.addCookie(cookie);
        resp.addCookie(cookie2);
        resp.getWriter().write("Cookie创建成功");


    }


    protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();
        Cookie iWangCookie = CookieUtil.findCookie(supplier.get(), cookies);
        Optional<Cookie> optional = Optional.ofNullable(iWangCookie);
        boolean b = optional.isPresent();
        //boolean b = (iWangCookie != null);
        JudgeTrueOrFalse judgeTrueOrFalse = WebUtil.judge(b);


        judgeTrueOrFalse.trueOrFalse(()->{
            try {
                resp.getWriter().write("找到了需要的cookie");
            } catch (IOException e) {
                e.printStackTrace();
            }
        },()->{
            try {
                resp.getWriter().write("找不到需要的cookie");
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

    }

    //修改cookie方法 一
    protected void updateCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //这里注意啊 value 不能由空格、圆括号，如果是中文需要进行base64 编码才可以的
        Cookie cookie = new Cookie(supplier.get(),"newValue1");
        resp.addCookie(cookie);
        resp.getWriter().write("Cookie修改成功了");

        Cookie[] cookies = req.getCookies();
        Cookie iWangCookie = CookieUtil.findCookie(supplier1.get(), cookies);

        boolean b = (iWangCookie != null);
        //获得了实现JudgeTrueOrFalse 这个接口的实现类
        JudgeTrueOrFalse judgeTrueOrFalse = WebUtil.judge(b);

//        judgeTrueOrFalse.trueOrFalse(() -> {
//            try {
//                iWangCookie.setValue("newValue2");
//                resp.addCookie(iWangCookie);
//                resp.getWriter().write("把"+supplier1.get()+"这个cookie 也修改了");
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        });




        judgeTrueOrFalse.trueOrFalse(()->{
            try {
                iWangCookie.setValue("newValue2");
                resp.addCookie(iWangCookie);
                resp.getWriter().write("把"+supplier1.get()+"这个cookie 也修改了");
            } catch (IOException e) {
                e.printStackTrace();
            }
        },()->{
            try {

                resp.getWriter().write("没有修改值");
            } catch (IOException e) {
                e.printStackTrace();
            }
        });



    };

    protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

         Cookie cookie = new Cookie("default","deFaultValue");
         resp.addCookie(cookie);
         resp.getWriter().write("设置了叫default的cookie");
    }
    //立马删除cookie
    protected void deleteNowCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       Cookie[] cookies = req.getCookies();
       Cookie iWantCookie = CookieUtil.findCookie(supplier.get(),cookies);
       boolean b = (iWantCookie != null);

        JudgeTrueOrFalse judgeTrueOrFalse = WebUtil.judge(b);
//        judgeTrueOrFalse.trueOrFalse(()->{
//            iWantCookie.setMaxAge(0);
//            resp.addCookie(iWantCookie);
//            try {
//                resp.getWriter().write("key1 这个cookie已经被删除");
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        });
//       JudgeTrueOrFalse judgeTrueOrFalse = WebUtil.judge(b);

        judgeTrueOrFalse.trueOrFalse(()->{
            iWantCookie.setMaxAge(0);
            resp.addCookie(iWantCookie);
            try {
                resp.getWriter().write("key1 这个cookie已经被删除");
            } catch (IOException e) {
                e.printStackTrace();
            }
        },()->{


            try {
                resp.getWriter().write("key1 这个cookie没有被删除");
            } catch (IOException e) {
                e.printStackTrace();
            }
        });


    }
    //设置cookie的过期时间6秒
    protected void cookieLife360(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Cookie cookie = new Cookie("cookie6","cookie6");
        cookie.setMaxAge(6);
        resp.addCookie(cookie);
        resp.getWriter().write("存活6秒的cookie 创建成功");
    }
    //cookie的path 学习
    protected void cookiePath(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("path","path");
        //req.getContextPath() 得到工程的路径
        /**
         * 这里注意下当浏览器访问http://ip+port/工程路径/abc/ 这样路径的请求时浏览器才会显示这个cookie
         * 否则看不到这个cookie
         *
         */
        cookie.setPath(req.getContextPath()+"/abc");
        resp.addCookie(cookie);
        resp.getWriter().write("创建了一个带有path 属性的cookie");

    }


}